package com.stackandqueue;

import java.util.Objects;
import java.util.Stack;

public class EvaluateReversePolishNotation0204_150 {
    public int evalRPN(String[] tokens) {
        Stack<Integer> sta = new Stack<>();

        for (int i = 0; i < tokens.length; i++) {
            if(Objects.equals(tokens[i], "+")){
                sta.push(sta.pop() + sta.pop());
            } else if (Objects.equals(tokens[i], "-")){
                sta.push(-sta.pop() + sta.pop());
            } else if (Objects.equals(tokens[i], "*")){
                sta.push(sta.pop() * sta.pop());
            } else if (Objects.equals(tokens[i], "/")){
                Integer pop1 = sta.pop();
                sta.push( sta.pop() / pop1);
            } else {
                sta.push(Integer.valueOf(tokens[i]));
            }
        }
        return sta.pop();
    }
}
